package com.zte.sys.area.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zte.sys.area.dao.IAreaDao;
import com.zte.sys.area.model.AreaCondition;
import com.zte.sys.area.model.SysCity;
import com.zte.sys.area.service.IAreaService;
import com.zte.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AreaService implements IAreaService {

    @Autowired
    private IAreaDao areaDao;

    /**
     * 区域分页条件查询
     * @param conn
     * @param pageModel
     * @return
     */
    @Override
    public PageModel<SysCity> queryCityPageByCondition(AreaCondition conn, PageModel<SysCity> pageModel) {
        if (conn.getPName().equals("null")){
            conn.setPName("");
        }
        if (conn.getCName().equals("null")){
            conn.setCName("");
        }
        int pageNum = pageModel.getPageNum();
        pageNum = pageNum<=0?1:pageNum;
        int pageSize = 3;
        PageHelper.startPage(pageNum,pageSize);
        List<SysCity> cities = areaDao.queryCityListByCondition(conn);
        PageInfo<SysCity> info = new PageInfo<SysCity>(cities);

        pageModel.setPageNum(info.getPageNum());
        pageModel.setPageSize(info.getPageSize());
        pageModel.setTotalPage(info.getPages());
        pageModel.setTotalCount((int)info.getTotal());
        pageModel.setData(new ArrayList<SysCity>(info.getList()));
        return pageModel;
    }

    @Override
    public List<SysCity> queryCityListByCondition(AreaCondition conn) {

        return areaDao.queryCityListByCondition(conn);
    }
}
